package org.example.jiaxiao.controller;

import org.example.jiaxiao.entity.Course;
import org.example.jiaxiao.entity.Student;
import org.example.jiaxiao.service.ArrangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/arrange")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class ArrangeController {

    @Autowired
    private ArrangeService arrangeService;

    /**
     * 获取学生信息列表，用于教练数据管理页面
     * @return 学生信息列表的响应实体
     */
    @GetMapping("/students")
    public ResponseEntity<List<Student>> getStudentinformation() {
        List<Student> students = arrangeService.getStudentinformation();
        if (students != null && !students.isEmpty()) {
            return ResponseEntity.ok(students);
        } else {
            return ResponseEntity.noContent().build();
        }
    }
    
    /**
     * 获取特定学生的课程信息，用于学员课程信息页面
     * @param studentId 学生ID
     * @return 学生课程信息的响应实体
     */
    @GetMapping("/student/{studentId}")
    public ResponseEntity<Map<String, Object>> getStudentCourseInfo(@PathVariable("studentId") Integer studentId) {
        Map<String, Object> studentInfo = arrangeService.getStudentCourseInfo(studentId);
        if (studentInfo != null && !studentInfo.isEmpty()) {
            return ResponseEntity.ok(studentInfo);
        } else {
            return ResponseEntity.noContent().build();
        }
    }

    /**
     * 安排学生课程（提交训练预约）
     * @param studentId 学生ID
     * @param courseId 课程ID
     * @return 操作结果的响应实体
     */
    @PostMapping("/course")
    public ResponseEntity<Map<String, String>> arrangeCourse(@RequestParam("studentId") Integer studentId, 
                                                         @RequestParam("courseId") Integer courseId) {
        boolean success = arrangeService.arrangeCourse(studentId, courseId);
        Map<String, String> response = new HashMap<>();
        if (success) {
            response.put("message", "课程安排成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "课程安排失败");
            return ResponseEntity.badRequest().body(response);
        }
    }

    /**
     * 根据筛选条件填充课程（批量安排）
     * @param filterParams 筛选参数
     * @return 操作结果的响应实体
     */
    @PostMapping("/fill")
    public ResponseEntity<Map<String, String>> FillCourse(@RequestBody Map<String, Object> filterParams) {
        boolean success = arrangeService.FillCourse(filterParams);
        Map<String, String> response = new HashMap<>();
        if (success) {
            response.put("message", "课程批量安排成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "课程批量安排失败");
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    /**
     * 获取所有课程列表，用于训练安排下拉选择
     * @return 课程列表的响应实体
     */
    @GetMapping("/courses")
    public ResponseEntity<List<Course>> getAllCourses() {
        List<Course> courses = arrangeService.getAllCourses();
        if (courses != null && !courses.isEmpty()) {
            return ResponseEntity.ok(courses);
        } else {
            return ResponseEntity.noContent().build();
        }
    }
}
